package com.kehutong.contact.service;

import com.kehutong.common.DoveClient;
import com.kehutong.common.util.Token;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.util.Objects;

/**
 * 数据权限服务类
 *
 * @author: liukunlong
 * @date: 2021-05-10 17:00
 */
public class DataPvService {
    static final Logger logger = LoggerFactory.getLogger(DataPvService.class);

    @Inject
    private DoveClient doveClient;

    /**
     * 构建登录用户可以查看的楼栋数据权限
     *
     * @param jsonObject 查询上下文对象
     */
    public void buildUserDataPv(Token token, JSONObject jsonObject) throws Exception {
        //如果是管理员则直接返回所有数据
        if(token.getRoles().contains("5")){
            jsonObject.put("buildNo",new JSONArray());
        }else{
            jsonObject.put("buildNo", getUserAssetsNo(token));
        }
    }

    /**
     * 根据员工编号获取员工信息
     * @param id 员工编号
     * @return
     * @throws Exception
     */
    public JSONObject getEmployee(String id) throws Exception{
        JSONObject employeeJson=new JSONObject();
        JSONObject employeeResult = doveClient.post("/basic/employee/service/get", (http) -> {
            http.addParam("id",id);
        });
        if (Objects.nonNull(employeeResult)) {
            if (employeeResult.getIntValue("code") == 200) {
                employeeJson = employeeResult.getJSONObject("data");
            }
        }
        return employeeJson;
    }
    /**
     * 获取当前登录人否则的楼栋编号
     * dataPvType 1 查询全部数据
     * dataPvType 2 查询我管理的
     * dataPvType 3 下属管理的(必须是上级身份)
     * @param token 用户信息
     * @return 楼栋编号数组
     * 如果没有相关权限返回NULL
     * @throws Exception 异常
     */
    public JSONArray getUserAssetsNo(Token token, String dataPvType) throws Exception {
        if(Objects.equal(dataPvType,"3")){
            //如果不是上级则放回NULL
            JSONObject employee = getEmployee(token.getUuid());
            JSONObject identityJson = employee.getJSONObject("identity");
            if(Objects.nonEmpty(employee)){
                if(!"1".equals(identityJson.getString("id"))){
                    return null;
                }
            }else{
                logger.info("获取员工信息为空.");
                return null;
            }
        }
        JSONArray result=new JSONArray();
        result.addAll(getUserAssetsNo(token));
        result.addAll(getUserShopClassifyNo(token));
        result.addAll(getUserParkingClassifyNo(token));
        return result;
    }

    /**
     * 获取当前登录人可以查看的的楼栋编号
     *
     * @param token 用户信息
     * @return 楼栋编号数组
     * @throws Exception 异常
     */
    public JSONArray getUserAssetsNo(Token token) throws Exception {
        JSONArray result = new JSONArray();
        JSONArray buildJsonArray = getBuildByEmployeeNos(token);
        if (Objects.nonEmpty(buildJsonArray)) {
            for (Object buildJsonObj : buildJsonArray) {
                JSONObject buildJson = (JSONObject) buildJsonObj;
                String id = buildJson.getString("id");
                result.add(id);
            }
        }else{
            result.add("-1");
        }
        return result;
    }


    /**
     * 获取当前登录人可以查看的的商铺分类编号
     *
     * @param token 用户信息
     * @return 楼栋编号数组
     * @throws Exception 异常
     */
    public JSONArray getUserShopClassifyNo(Token token) throws Exception {
        JSONArray result = new JSONArray();
        JSONArray buildJsonArray = getShopClassifyByEmployeeNos(token);
        if (Objects.nonNull(buildJsonArray)) {
            for (Object buildJsonObj : buildJsonArray) {
                JSONObject buildJson = (JSONObject) buildJsonObj;
                String id = buildJson.getString("id");
                result.add(id);
            }

        }
        return result;
    }

    /**
     * 获取当前登录人可以查看的的车位分类编号
     *
     * @param token 用户信息
     * @return 楼栋编号数组
     * @throws Exception 异常
     */
    public JSONArray getUserParkingClassifyNo(Token token) throws Exception {
        JSONArray result = new JSONArray();
        JSONArray buildJsonArray = getParkingClassifyByEmployeeNos(token);
        if (Objects.nonNull(buildJsonArray)) {
            for (Object buildJsonObj : buildJsonArray) {
                JSONObject buildJson = (JSONObject) buildJsonObj;
                String id = buildJson.getString("id");
                result.add(id);
            }

        }
        return result;
    }

    /***
     * 获取当前等于用户管理的楼栋信息
     * @param token 用户信息
     * @return 楼栋集合
     * @throws Exception 异常
     */
    private JSONArray getBuildByEmployeeNos(Token token) throws Exception {
        JSONObject result = doveClient.post("/basic/building/service/list", http -> {
            JSONObject body = new JSONObject();
            body.put("employeeNos", token.getUuid());
            http.addHeader("companyNo", token.getCompanyNo());
            http.setBody(body.toString());
        });

        JSONArray data = result.getJSONArray("data");
        if (Objects.isNull(data)) {
            return null;
        }
        return data;
    }

    /***
     * 获取当前等于用户管理的商铺分类信息
     * @param token 用户信息
     * @return 楼栋集合
     * @throws Exception 异常
     */
    private JSONArray getShopClassifyByEmployeeNos(Token token) throws Exception {
        JSONObject result = doveClient.post("/basic/shop/classify/service/list", http -> {
            JSONObject body = new JSONObject();
            body.put("employeeNos", token.getUuid());
            http.addHeader("companyNo", token.getCompanyNo());
            http.setBody(body.toString());
        });

        JSONArray data = result.getJSONArray("data");
        if (Objects.isNull(data)) {
            return null;
        }
        return data;
    }

    /***
     * 获取当前等于用户管理的车位分类信息
     * @param token 用户信息
     * @return 楼栋集合
     * @throws Exception 异常
     */
    private JSONArray getParkingClassifyByEmployeeNos(Token token) throws Exception {
        JSONObject result = doveClient.post("/basic/parking/classify/service/list", http -> {
            JSONObject body = new JSONObject();
            body.put("employeeNos", token.getUuid());
            http.addHeader("companyNo", token.getCompanyNo());
            http.setBody(body.toString());
        });

        JSONArray data = result.getJSONArray("data");
        if (Objects.isNull(data)) {
            return null;
        }
        return data;
    }

}
